<?php

function retCust($config,$id){
	$name = '';
	$db =  new db($config);
	//JUMLAH di GUDANG
	$result = $db->query("SELECT nama FROM mn_customer WHERE idCustomer = '$id'");
	if ($db->rowCount()>0)
		$name = $result[0]['nama'];
	return $name;
}

function view_embozz($config,$param) {
    view_header_up($config,$param);
    //
    ?>
    <script type="text/javascript" src="<? echo $config['js_url']; ?>jquery-1.8.2.min.js"></script>
    <title>Idebeda-Produksi Embozz</title>
    <?
    //
    view_header_bottom($config,$param);
    ?>
    <h2>Produksi Embozz</h2>
    <script type="text/javascript">
		function showHide(id){
			$('#'+id).toggle('slow');
		}
		function getCust(nohp) {		 
			form = document.getElementById('ffpesan');
			var code = '';
			var nama = '';
			var prov = '';
			var kabkot = '';
			var alamat = '';
			var kodepos = '';
			
			xmlhttp=mintajax();
			xmlhttp.onreadystatechange=function()
			{
			  if (xmlhttp.readyState== 4 && xmlhttp.status== 200)
			  {
				var result = eval("("+xmlhttp.responseText+")");
				code = result['code'];
				if(code >=0)
				{
					//ada
					alert('Pelanggan ditemukan');
					form.nama.value = result['nama'];
					form.prov.value = result['prov'];
					form.kota.value = result['kabkot'];
					form.lengkap.value = result['alamat'];
					form.pobox.value = result['kodepos'];
				}
			  }
			}
			xmlhttp.open("GET","<? echo $config['home_url']."produksi/cust"; ?>?nohp="+nohp,true);
			xmlhttp.send(null);
			//$('#fffpesan').show('slow');
		}
		function cekPesanan(){
			var key = document.getElementById('qword').value;
			var msg = document.getElementById('cpesan-val');
			
			xmlhttp=mintajax();
			xmlhttp.onreadystatechange=function()
			{
			  if (xmlhttp.readyState== 4 && xmlhttp.status== 200)
			  {
				//alert('Ditemukan');
				msg.innerHTML = xmlhttp.responseText;
			  }
			}
			xmlhttp.open("GET","<? echo $config['home_url']."produksi/embozz/cek"; ?>?qword="+key,true);
			xmlhttp.send(null);
		}
		function validateEmpty(fld) {
			var error = "";
		 
			if (fld.value.length == 0) {
				fld.style.background = 'Yellow'; 
				error = "1";
			} else {
				fld.style.background = 'White';
			}
			return error;  
		}
		function validateForm(theForm) {
			var reason = "";
			
			  reason += validateEmpty(theForm.nama);
			  reason += validateEmpty(theForm.qty);
			  //reason += validateEmpty(theForm.prov);
			  reason += validateEmpty(theForm.kota);
			  //reason += validateEmpty(theForm.pobox);
			  reason += validateEmpty(theForm.lengkap);
				  
			  if (reason != "") {
				alert("Perhatian, isi yang masih kosong.");
				return false;
			  }

			  return true;
		}
	    $('#qword').keypress(function(event) {
			if (event.keyCode == 13) {
				alert('Entered');
			}
		});
		function confAction(ulink,msg){
			var del=confirm("Apakah Anda ingin "+msg+" ?");
			if (del==true){
			  window.location.assign(ulink);
			}
		}
	</script>
	<?
	if (isset($_SESSION['addsucc'])) {
        echo '<div class="success alert">'.$_SESSION['addsucc'].'</div>';
        unset($_SESSION['addsucc']);
    }
    if (isset($_SESSION['addfail'])) {
        echo '<div class="warning alert">'.$_SESSION['addfail'].'</div>';
        unset($_SESSION['addfail']);
    }
    ?>
    <?php
		if (isset($_SESSION['logged']))
			if ($_SESSION['logged']['role']<3){
    ?>
    <div onclick="showHide('fpesan')"><h4>Tambah Pesanan</h4></div>
    <div id="fpesan" style="display:none">
		<a target="_blank" href="<?php echo $config['home_url']."info/"; ?>">(*info-twitter)</a>
		<form action="" id="ffpesan" method="post" onsubmit="return validateForm(this)">
			<ul>
				<li class="prepend field">
					<label class="inline" for="nohp">Nomor Pelanggan*</label>
					<span class="adjoined">+62</span>
					<input class="narrow text input" id="nohp" name="nohp" type="text" placeholder="nomor handphone" onchange="getCust(this.value)">
				</li>
				<li class="field">
					<label class="inline" for="nama">Nama Tujuan*</label>
					<input class="wide text input" id="nama" name="nama" type="text" placeholder="nama pemesan" />
				</li>
				<li class="field">
					<label class="inline" for="qty">Jumlah*</label>
					<input class="xnarrow text input" id="qty" name="qty" type="text" placeholder="" />
				</li>
				<li class="field">
					<label class="inline" for="prov">Provinsi Tujuan</label>
					<input class="normal text input" id="prov" name="prov" type="text" placeholder="prov" />
				</li>
				<li class="field">
					<label class="inline" for="kota">Kota Tujuan*</label>
					<input class="normal text input" id="kota" name="kota" type="text" placeholder="kota" />
				</li>
				<li class="field">
					<label class="inline" for="pobox">Kodepos Tujuan</label>
					<input class="xnarrow text input" id="pobox" name="pobox" type="text" placeholder="kodepos" />
				</li>
				<li class="field">
					<label class="inline" for="lengkap">Alamat Tujuan*</label>
					<textarea class="input textarea" rows="3" placeholder="Alamat lengkap" name="lengkap" id="lengkap"></textarea>
				</li>
				<div class="medium primary btn"><input type="submit" name="tambah" value="Tambah" /></div>
			</ul>
		</form>
	</div>
	<div onclick="showHide('cpesan')"><h4>Cek Pesanan</h4></div>
	<div id="cpesan">
		<ul>
			<li><i class="icon-info"> : 'nama' atau 'nomor pesanan'</i></li>
			<li class="field">
				<input class="narrow text input" id="qword" name="qword" type="text" placeholder="keywords" >
				<span class="medium primary btn icon-right icon-search" onclick="cekPesanan()">Cek</span>
			</li>
		</ul>
		<div id="cpesan-val"></div>
	</div>
	<div onclick="showHide('iprod')"><h4>Info Produksi</h4></div>
	<div id="iprod">
		<?php
			$db =  new db($config);
			//JUMLAH di GUDANG
			$result = $db->query("SELECT stok FROM mn_gudang WHERE produk = 'embozz'");
			$gudang = $result[0]['stok'];
			if ($_SESSION['logged']['id']!='4'){
				echo "<div><h5>Tersedia di gudang : $gudang buah</h5></div>";
			}else{
				echo "<div><h5>Tersedia di gudang : $gudang buah </h5><a href=\"".$config['home_url']."produksi/embozz/gudang/"."\"><i class=\"icon-pencil\">[Ubah]</i></a></div>";
			}
			//JUMLAH PESANAN
			$result = $db->query("SELECT SUM(qty) as total FROM mn_embozz WHERE status < '3'");
			$total = ($result[0]['total']=='') ? 0 : $result[0]['total'];
			//HARUS PRODUKSI
			$selisih = $total - $gudang;
			if ($selisih > 0){
				echo "<div><h5><font color=\"red\">Kekurangan produksi : $selisih buah</font></h5></div>";
			}
			echo "<div onclick=\"showHide('iprod1')\"><h5>Jumlah pesanan : $total buah</h5></div>";
			echo "<div id=\"iprod1\" style=\"display:none\">";
			$result = $db->query("SELECT * FROM mn_embozz WHERE status = '1' ORDER BY idEmbozz ASC");
				echo "<ol>";
				foreach ($result as $pesanan){
					$id = $pesanan['idEmbozz'];
					echo "<li>";
						if (($_SESSION['logged']['id']=='3') OR ($_SESSION['logged']['id']=='4') OR ($_SESSION['logged']['id']==$pesanan['modifier'])){
							echo "<a target=\"_blank\" href=\"".$config['home_url']."pelanggan/id/".$pesanan['idCust']."\">".retCust($config,$pesanan['idCust'])."</a> (".date('d/m/Y', strtotime($pesanan['tgl_pesan'])).")<a target=\"_blank\" href=\"".$config['home_url']."produksi/embozz/id/$id\"> #".$pesanan['idEmbozz']."</a> :<font color=\"orange\"> ".$pesanan['qty']." buah.</font> by ".getCrew($config,$pesanan['modifier']);
							//JADI act
							echo "<a href=\"javascript:confAction('".$config['home_url']."produksi/embozz/".$pesanan['idEmbozz']."/bayar-jadi','ubah status #".$pesanan['idEmbozz']." sudah bayar')\"><i class=\"icon-check\"></i>Konf Bayar, </a>";
							//BATAL act
							echo "<a href=\"javascript:confAction('".$config['home_url']."produksi/embozz/".$pesanan['idEmbozz']."/batal','membatalkan #".$pesanan['idEmbozz']."')\"><i class=\"icon-cancel\"></i>Batal</a>";
						}else{
							echo "<a target=\"_blank\" href=\"".$config['home_url']."pelanggan/id/".$pesanan['idCust']."\">".retCust($config,$pesanan['idCust'])."</a> (".date('d/m/Y', strtotime($pesanan['tgl_pesan'])).") #".$pesanan['idEmbozz']." :<font color=\"orange\"> ".$pesanan['qty']." buah.</font> by ".getCrew($config,$pesanan['modifier']);
						}
					echo "</li>";
				};
				echo "</ol>";
			echo "</div>";
			//HARUS BAYAR
			/*$total = 0;
			$result = $db->query("SELECT count(*) as total FROM mn_embozz WHERE bayar = '0' AND status = '2'");
			$total = $result[0]['total'];
			echo "<div onclick=\"showHide('ibayar')\"><h5>Belum konfirmasi bayar : $total pesanan</h5></div>";
			echo "<div id=\"ibayar\" style=\"display:none\">";
			$result = $db->query("SELECT * FROM mn_embozz WHERE bayar = '0' AND status = '2' ORDER BY idEmbozz ASC");
				echo "<ol>";
				foreach ($result as $pesanan){
					$id = $pesanan['idEmbozz'];
					echo "<li>";
						echo "<div><a target=\"_blank\" href=\"".$config['home_url']."pelanggan/id/".$pesanan['idCust']."\">".$pesanan['nama']."</a> (".date('d/m/Y', strtotime($pesanan['tgl_pesan'])).")<a target=\"_blank\" href=\"".$config['home_url']."produksi/embozz/id/$id\"> #".$pesanan['idEmbozz']."</a> : ".$pesanan['qty']." buah. by ".getCrew($config,$pesanan['modifier'])."</div>";
						echo "<div>\"".$pesanan['alamat']."\", ".$pesanan['kabkot'].", ".$pesanan['prov']." - ".$pesanan['kodepos'];
						//BAYAR act
						echo "<a href=\"javascript:confAction('".$config['home_url']."produksi/embozz/".$pesanan['idEmbozz']."/bayar','ubah status #".$pesanan['idEmbozz']." sudah bayar')\"><i class=\"icon-check\"></i>Bayar, </a>";
						//BATAL act
						echo "<a href=\"javascript:confAction('".$config['home_url']."produksi/embozz/".$pesanan['idEmbozz']."/batal','membatalkan pesanan')\"><i class=\"icon-cancel\"></i>Batal</a>";
					echo "</li>";
				};
				echo "</ol>";
			echo "</div>";*/
			//HARUS KIRIM
			$total = 0;
			$result = $db->query("SELECT count(*) as total FROM mn_embozz WHERE status = '2' AND bayar = '1'");
			$total = $result[0]['total'];
			echo "<div onclick=\"showHide('iprod2')\"><h5>Siap kirim : $total pesanan</h5></div>";
			echo "<div id=\"iprod2\" style=\"display:none\">";
			$result = $db->query("SELECT * FROM mn_embozz WHERE status = '2' AND bayar = '1' ORDER BY idEmbozz ASC");
				echo "<ol>";
				foreach ($result as $pesanan){
					$id = $pesanan['idEmbozz'];
					echo "<li>";
					if (($_SESSION['logged']['id']=='3') OR ($_SESSION['logged']['id']=='4') OR ($_SESSION['logged']['id']==$pesanan['modifier'])){
						echo "<div><a target=\"_blank\" href=\"".$config['home_url']."pelanggan/id/".$pesanan['idCust']."\">".retCust($config,$pesanan['idCust'])."</a> (".date('d/m/Y', strtotime($pesanan['tgl_pesan'])).") <a target=\"_blank\" href=\"".$config['home_url']."produksi/embozz/id/$id\"> #".$pesanan['idEmbozz']."</a> : <font color=\"orange\">".$pesanan['qty']." buah</font>. by ".getCrew($config,$pesanan['modifier'])."</div>";
						echo "<div>Tujuan : ".$pesanan['nama']." , \"".$pesanan['alamat']."\", ".$pesanan['kabkot'].", ".$pesanan['prov']." - ".$pesanan['kodepos'];
						//KIRIM
						echo "<a href=\"javascript:confAction('".$config['home_url']."produksi/embozz/".$pesanan['idEmbozz']."/kirim','ubah status #".$pesanan['idEmbozz']." sudah kirim')\"><i class=\"icon-check\"></i>Sudah Kirim </a>";
						//BATAL act
						//echo "<a href=\"javascript:confAction('".$config['home_url']."produksi/embozz/".$pesanan['idEmbozz']."/batal','membatalkan pesanan')\"><i class=\"icon-cancel\"></i>Batal</a>";
					}else{
							echo "<div><a target=\"_blank\" href=\"".$config['home_url']."pelanggan/id/".$pesanan['idCust']."\">".retCust($config,$pesanan['idCust'])."</a> (".date('d/m/Y', strtotime($pesanan['tgl_pesan'])).") <font color=\"orange\">#".$pesanan['idEmbozz']."</font> : ".$pesanan['qty']." buah. by ".getCrew($config,$pesanan['modifier'])."</div>";
							echo "<div>Tujuan : ".$pesanan['nama']." , \"".$pesanan['alamat']."\", ".$pesanan['kabkot'].", ".$pesanan['prov']." - ".$pesanan['kodepos'];
						}
					echo "</li>";
				};
				echo "</ol>";
			echo "</div>";
			//
		?>
	</div>
	<div onclick="showHide('istat')"><h4><a href="#">Statistik</a></h4></div>
	<div id="istat">
	<?php
	//TERJUAL
	$total = 0;
	$result = $db->query("SELECT count(*) as total FROM mn_embozz WHERE status = '3' AND bayar = '1'");
	$total = $result[0]['total'];
	echo "<div><h5>Pesanan telah selesai : $total pesanan</h5></div>";
	$total = 0;
	$result = $db->query("SELECT sum(qty) as total FROM mn_embozz WHERE status = '3' AND bayar = '1'");
	$total = $result[0]['total'];
	echo "<div><h5>Embozz terjual : $total buah</h5></div>";
	//BATAL
	$total = 0;
	$result = $db->query("SELECT count(*) as total FROM mn_embozz WHERE status = '4'");
	$total = $result[0]['total'];
	echo "<div><h5>Pesanan batal : $total pesanan</h5></div>";
	?>
	</div>
	<?php
		}
    ?>
<?
	view_footer($config,$param);
}

function view_pembozz($config,$param,$id) {
    view_header_up($config,$param);
    //
    ?>
    <script type="text/javascript" src="<? echo $config['js_url']; ?>jquery-1.8.2.min.js"></script>
    <title>Idebeda-Produksi Embozz</title>
    <?
    //
    view_header_bottom($config,$param);
    ?>
    <h2>Edit Pesanan Embozz</h2>
    <script type="text/javascript">
		function validateEmpty(fld) {
			var error = "";
		 
			if (fld.value.length == 0) {
				fld.style.background = 'Yellow'; 
				error = "1";
			} else {
				fld.style.background = 'White';
			}
			return error;  
		}
		function validateForm(theForm) {
			var reason = "";
			
			  reason += validateEmpty(theForm.nama);
			  reason += validateEmpty(theForm.qty);
			  //reason += validateEmpty(theForm.prov);
			  reason += validateEmpty(theForm.kota);
			  //reason += validateEmpty(theForm.pobox);
			  reason += validateEmpty(theForm.lengkap);
				  
			  if (reason != "") {
				alert("Perhatian, isi yang masih kosong.");
				return false;
			  }

			  return true;
		}
	</script>
    <?php
		if (isset($_SESSION['logged']))
			if ($_SESSION['logged']['role']<3){
			$db =  new db($config);
			$result = $db->query("SELECT * FROM mn_embozz WHERE idEmbozz = '".urlencode($id)."'");
    ?>
    <div><h4>No Pesanan : <? echo $result[0]['idEmbozz']; ?></h4></div>
	<form action="" id="ffpesan" method="post" onsubmit="return validateForm(this)">
		<ul>
			<input type="hidden" name="idc" value="<? echo $result[0]['idCust']; ?>" />
			<input type="hidden" name="qtyold" value="<? echo $result[0]['qty']; ?>" />
			<li class="field">
				<label class="inline" for="nama">Nama Tujuan</label>
				<input class="wide text input" id="nama" name="nama" type="text" placeholder="nama pemesan" value="<? echo $result[0]['nama']; ?>" />
			</li>
			<li class="field">
				<label class="inline" for="qty">Jumlah</label>
				<input class="xnarrow text input" id="qty" name="qty" type="text" placeholder="" value="<? echo $result[0]['qty']; ?>" />
			</li>
			<li class="field">
				<label class="inline" for="prov">Provinsi Tujuan</label>
				<input class="normal text input" id="prov" name="prov" type="text" placeholder="prov" value="<? echo $result[0]['prov']; ?>" />
			</li>
			<li class="field">
				<label class="inline" for="kota">Kota Tujuan</label>
				<input class="normal text input" id="kota" name="kota" type="text" placeholder="kota" value="<? echo $result[0]['kabkot']; ?>" />
			</li>
			<li class="field">
				<label class="inline" for="pobox">Kodepos Tujuan</label>
				<input class="xnarrow text input" id="pobox" name="pobox" type="text" placeholder="kodepos" value="<? echo $result[0]['kodepos']; ?>" />
			</li>
			<li class="field">
				<label class="inline" for="lengkap">Alamat Tujuan</label>
				<textarea class="input textarea" rows="3" placeholder="Alamat lengkap" name="lengkap" id="lengkap"><? echo $result[0]['alamat']; ?></textarea>
			</li>
			<div class="medium primary btn"><input type="submit" name="edit" value="Edit" /></div>
		</ul>
	</form>
	<?php
		}
    ?>
<?
	view_footer($config,$param);
}

function view_gembozz($config,$param) {
    view_header_up($config,$param);
    //
    ?>
    <script type="text/javascript" src="<? echo $config['js_url']; ?>jquery-1.8.2.min.js"></script>
    <title>Idebeda-Gudang Embozz</title>
    <?
    //
    view_header_bottom($config,$param);
    ?>
    <h2>Gudang Embozz</h2>
    <script type="text/javascript">
		function validateEmpty(fld) {
			var error = "";
		 
			if (fld.value.length == 0) {
				fld.style.background = 'Yellow'; 
				error = "1";
			} else {
				fld.style.background = 'White';
			}
			return error;  
		}
		function validateForm(theForm) {
			var reason = "";
			
			  reason += validateEmpty(theForm.nama);
			  reason += validateEmpty(theForm.qty);
			  //reason += validateEmpty(theForm.prov);
			  reason += validateEmpty(theForm.kota);
			  //reason += validateEmpty(theForm.pobox);
			  reason += validateEmpty(theForm.lengkap);
				  
			  if (reason != "") {
				alert("Perhatian, isi yang masih kosong.");
				return false;
			  }

			  return true;
		}
	</script>
    <?php
		if (isset($_SESSION['logged']))
			if ($_SESSION['logged']['role']<3){
			$db =  new db($config);
			$result = $db->query("SELECT * FROM mn_gudang WHERE produk = 'embozz'");
			$stok = $result[0]['stok'];
			if (isset($_SESSION['succgudang'])) {
				echo '<div class="success alert">'.$_SESSION['succgudang'].'</div>';
				unset($_SESSION['succgudang']);
			}
    ?>
    <div><h4>Stok di gudang : <?php echo $stok; ?> buah</h4></div>
    <div><h4>Tambah ke gudang</h4></div>
    <div>
		<form method="post" action="">
			<ul>
				<li class="append field">
					<input class="xnarrow text input" id="qty" name="qty" type="text" placeholder="0" /><span class="adjoined">buah</span>
				</li>
				<div class="medium secondary btn"><input type="submit" name="gudangplus" value="Tambah" /></div>
			</ul>
		</form>
    </div>
    <div><h4>Ambil dari gudang</h4></div>
    <div>
		<form method="post" action="">
			<ul>
				<li class="append field">
					<input class="xnarrow text input" id="qty" name="qty" type="text" placeholder="0" /><span class="adjoined">buah</span>
				</li>
				<div class="medium danger btn"><input type="submit" name="gudangminus" value="Ambil" /></div>
			</ul>
		</form>
    </div>
	<?php
		}
    ?>
<?
	view_footer($config,$param);
}

?>
